#include <func.h>
typedef struct shardRes_s
{
    int flag;
    int count;
    pthread_mutex_t MuTex;
    pthread_cond_t cond;

} shardRes_t;

void *SellTicket1(void *arg)
{
   sleep(1);
    printf("=====================窗口1=================\n");
    shardRes_t *pshard = (shardRes_t *)arg;
    while (1)
    {
        pthread_mutex_lock(&pshard->MuTex);
        if (pshard->count > 0)
        {
            printf("Before 1shell , ticket=%d\n", pshard->count);

            --pshard->count;
            printf("after 1shell , ticket=%d\n", pshard->count);

            if (pshard->count == 0 && pshard->flag == 0)
            {
                pthread_cond_signal(&pshard->cond);
                pshard->flag = 1;
            }
            else if (pshard->count == 0 && pshard->flag != 0)
            {
                printf("没有票了1\n");

                pthread_mutex_unlock(&pshard->MuTex);
                break;
            }
        }

        else
                
        {
                printf("1kou没有票了\n");
            pthread_mutex_unlock(&pshard->MuTex);
            break;
        }
        pthread_mutex_unlock(&pshard->MuTex);
    }
    pthread_exit(NULL);
}

void *SellTicket2(void *arg)
{
    sleep(1);
    printf("=====================窗口2=================\n");
    shardRes_t *pshard = (shardRes_t *)arg;
    while (1)
    {
        pthread_mutex_lock(&pshard->MuTex);
        if (pshard->count > 0)
        {
            printf("Before 2shell , ticket=%d\n", pshard->count);

            --pshard->count;
            printf("after 2shell , ticket=%d\n", pshard->count);

            if (pshard->count == 0 && pshard->flag == 0)
            {
                pthread_cond_signal(&pshard->cond);
                pshard->flag = 1;
            }
            else if (pshard->count == 0 && pshard->flag != 0)
            {
                printf("没有票了2\n");
                pthread_mutex_unlock(&pshard->MuTex);
                break;
            }
        }

        else
        {
                printf("2kou没有票了\n");
            
            pthread_mutex_unlock(&pshard->MuTex);
            break;
        }
        pthread_mutex_unlock(&pshard->MuTex);
    }
    pthread_exit(NULL);
}
void *addTicket(void *arg)
{
    shardRes_t *pshard = (shardRes_t *)arg;
    pthread_mutex_lock(&pshard->MuTex);
    if (pshard->flag != 1)
    {
        pthread_cond_wait(&pshard->cond, &pshard->MuTex);
    }
    printf("=============================================================add ticket\n");
    pshard->count = 10;
    pthread_mutex_unlock(&pshard->MuTex);

    pthread_exit(NULL);
}

// thread 的创建删除取消 锁的互斥 同步原理  生产者消费者(卖票)  变量
// wait 与 signal   wait 先开始运行 然后在signal,就是说卖票的进入等待
int main()
{
    shardRes_t shareRes;
    shareRes.count = 200;
    shareRes.flag = 0;
    printf("主线程\n");
    pthread_mutex_init(&shareRes.MuTex, NULL);
    pthread_cond_init(&shareRes.cond, NULL);
    pthread_t tid1, tid2, tid3;
    pthread_create(&tid1, NULL, SellTicket1, (void *)&shareRes);
    pthread_create(&tid1, NULL, SellTicket2, (void *)&shareRes);
    pthread_create(&tid3, NULL, addTicket, (void *)&shareRes);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);

    pthread_mutex_destroy(&shareRes.MuTex);
    pthread_cond_destroy(&shareRes.cond);
    return 0;
}